﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using TestFireBroadcastingHostPhone.RecordData;

namespace TestFireBroadcastingHostPhone.ViewModel
{
    public class MainWindowMV : ViewModelBase
    {
        /// <summary>
        /// 打开菜单栏的开关
        /// </summary>
        public bool open_TabItem = false;

        public int _TabItemLength;
        /// <summary>
        /// TabItem的长度
        /// </summary>
        public int TabItemLength
        {
            get { return _TabItemLength; }
            set
            {
                if (_TabItemLength != value)
                {
                    _TabItemLength = value;
                    OnPropertyChanged();
                }
            }
        }

        public Visibility _Visibility_ManualTestWindow;
        /// <summary>
        /// 隐藏手动调试界面属性
        /// </summary>
        public Visibility Visibility_ManualTestWindow
        {
            get { return _Visibility_ManualTestWindow; }
            set
            {
                if (_Visibility_ManualTestWindow != value)
                {
                    _Visibility_ManualTestWindow = value;
                    OnPropertyChanged();
                }
            }
        }

        public Visibility _Visibility_InstrumentsConfigWindow;
        /// <summary>
        /// 仪器调试界面隐藏属性
        /// </summary>
        public Visibility Visibility_InstrumentsConfigWindow
        {
            get { return _Visibility_InstrumentsConfigWindow; }
            set
            {
                if (_Visibility_InstrumentsConfigWindow != value)
                {
                    _Visibility_InstrumentsConfigWindow = value;
                    OnPropertyChanged();
                }
            }
        }

        public Visibility _Visibility_UserConfigWindow;
        /// <summary>
        /// 隐藏用户配置界面属性
        /// </summary>
        public Visibility Visibility_UserConfigWindow
        {
            get { return _Visibility_UserConfigWindow; }
            set
            {
                if (_Visibility_UserConfigWindow != value)
                {
                    _Visibility_UserConfigWindow = value;
                    OnPropertyChanged();
                }
            }
        }


        /// <summary>
        /// 绑定打开TabItem菜单栏事件
        /// </summary>
        public ICommand Btn_OpenCloseTabItemMenu_Command => new RelayCommand(OnBtn_OpenCloseTabItemMenu);
        /// <summary>
        /// 打开TabItem菜单栏
        /// </summary>
        /// <param name="obj"></param>
        private void OnBtn_OpenCloseTabItemMenu(object obj)
        {
            if (!open_TabItem)
            {
                TabItemLength = 100;
                open_TabItem = true;
            }
            else
            {
                TabItemLength = 1;
                open_TabItem= false;
            }
        }

        /// <summary>
        /// 绑定主界面窗体载入函数界面
        /// </summary>
        public ICommand MainWindow_LoadedCommand => new RelayCommand(MainWindow_Loaded);
        /// <summary>
        /// 主界面窗体载入函数
        /// </summary>
        /// <param name="obj"></param>
        private void MainWindow_Loaded(object obj)
        {
            //不是管理员登录的缺陷
            if (!UserLoadingMV.userLoadingMV.administrator_loading)
            {
                Visibility_ManualTestWindow = Visibility.Hidden;//不可以进入手动测试界面
                Visibility_InstrumentsConfigWindow = Visibility.Hidden;//不可以进入仪器调试界面
                Visibility_UserConfigWindow = Visibility.Hidden;//不可以进入用户设置界面
            }
            //如果日志文件不存在，按照该默认路径位置创建文件
            if (!File.Exists(LogsQueryMV.logsdata))
            {
                // 如果目录不存在，则创建目录
                string directoryPath = System.IO.Path.GetDirectoryName(LogsQueryMV.logsdata);
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                // 创建文件
                using (FileStream fs = File.Create(LogsQueryMV.logsdata))
                {
                    // 可以在这里初始化文件，例如写入标题等
                }
            }
            AutoTestConfig.Default.测试日期 = DateTime.Now.ToString("yyyy-MM-dd");
            AutoTestConfig.Default.测试人员 = UserLoadingMV.userLoadingMV.Text_UserName;
            AutoTestConfig.Default.Save();
            LogsQueryMV.logsQueryMV.ImportTxtToDataGrid(LogsQueryMV.logsdata);//读取日志文件中的记录信息
            string currentDirectory = Directory.GetCurrentDirectory() + "\\Product"; ;
            string exprisePath = System.IO.Path.Combine(currentDirectory, "ProductType.xlsx");
            if (File.Exists(exprisePath))
            {
                LogsQueryMV.logsQueryMV.ImportExcelToDataGrid(exprisePath);
            }
            SystemConfigMV.systemconfigMV.SystemConfig_Loaded();//执行参数配置界面初始化
            AutoTestMV.autoTestMVInstance.OnAutoTest_Loaded();//执行自动测试界面初始化
        }
    }
}
